package com.weiyuwqj.controller.index;

import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.model.vo.BasDebtVO;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.model.vo.BasItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ROGSTAR on 2016/11/29.
 * pc首页
 */
@Controller
public class IndexController {

    @Autowired
    BusInvestService investService;
    @Autowired
    BasUserService userService;
    @Autowired
    SysPictureService pictureService;
    @Autowired
    BasItemService itemService;
    @Autowired
    SysArticleService articleService;
    @Autowired
    BusInvestRepayService investRepayService;
    @Autowired
    BasDebtService debtService;
    @Autowired
    BusAccountService accountService;

    /**
     * 首页
     * @param request
     * @return
     */
    @RequestMapping("/index")
    public Object index(HttpServletRequest request){
        /*//平台累计投资金额
        BigDecimal investAmount=investService.sumInvestAmount();
        //平台注册人数
        Integer userNum=userService.countUserNum();
        //当日投资金额
        Map investMap=investService.countInvestAmountByTime();
        String todayAmount = "0";
        if(investMap!=null){
            todayAmount = investMap.get("todayAmount").toString();
        }
        //带来的收益
        Map interestParam = new HashMap();
        Map interestMap = investRepayService.statRepay(interestParam);
        String repayItemInterest = interestMap.get("repayItemInterest").toString();

        //增加数据
        investAmount=investAmount.add(new BigDecimal(2000000.00));
        userNum=userNum+5000;
        repayItemInterest=new BigDecimal(repayItemInterest).add(new BigDecimal(19000.00)).toString();

        //首页滚动图
        List<SysPicture> pictureList=pictureService.getMainPicture(1);
        //新手标
        List<BasItemVo> isNewItemList = itemService.getIsNewItemList();
        //查询2个短标
        List<BasItemVo> shortItemList = itemService.getIndexShortItemList();
        //查询2个长标
        List<BasItemVo> longItemList = itemService.getIndexLongItemList();
//        //查询两个债权转让
//        List<BasDebtVO> debtList = debtService.getIndexDebtList();

        Map map=new HashMap();
        map.put("siteId",8);
        Paginator p=new Paginator();
        p.setPageNum(1);
        p.setPageSize(3);
        PageList hyxxArticles=articleService.getListAndPicture(p,map);

        map.put("siteId",9);
        p.setPageSize(5);
        PageList ptdtArticles=articleService.getListAndPicture(p,map);

        map.put("siteId",3);
        p.setPageSize(99);
        PageList hzhbArticles=articleService.getListAndPicture(p,map);

        //获取userId
        Object userId = request.getSession().getAttribute("userId");
        if(userId != null){
            BusAccount account = accountService.getBusAccountByUserId(Integer.parseInt(userId.toString()));
            request.getSession().setAttribute("account",account);
        }

        //统计每个标种的数量
        Map itemNum = itemService.countItemInvestNum();
        //统计可投资债权的数量
        Map debtNum =debtService.countDebtInvestNum();

        request.getSession().setAttribute("shortItemList",shortItemList);
        request.getSession().setAttribute("longItemList",longItemList);
        request.getSession().setAttribute("investAmount",investAmount);
        request.getSession().setAttribute("userNum",userNum);
        request.getSession().setAttribute("pictureList",pictureList);
        request.getSession().setAttribute("isNewItemList",isNewItemList);
        request.getSession().setAttribute("hyxxArticles",hyxxArticles);
        request.getSession().setAttribute("ptdtArticles",ptdtArticles);
        request.getSession().setAttribute("hzhbArticles",hzhbArticles);
        request.getSession().setAttribute("todayAmount",todayAmount);
        request.getSession().setAttribute("repayItemInterest",repayItemInterest);
        request.getSession().setAttribute("itemNum",itemNum);
        request.getSession().setAttribute("debtNum",debtNum);*/
        //平台累计投资金额
        BigDecimal investAmount=BigDecimal.valueOf(10000);
        //平台注册人数
        Integer userNum=1000;
        //当日投资金额
        Map investMap=investService.countInvestAmountByTime();
        String todayAmount = "0";
        if(investMap!=null){
            todayAmount = investMap.get("todayAmount").toString();
        }
        //带来的收益
        Map interestParam = new HashMap();
        Map interestMap = investRepayService.statRepay(interestParam);
        String repayItemInterest = interestMap.get("repayItemInterest").toString();

        //增加数据
        investAmount=investAmount.add(new BigDecimal(2000000.00));
        userNum=userNum+5000;
        repayItemInterest=new BigDecimal(repayItemInterest).add(new BigDecimal(19000.00)).toString();

        //首页滚动图
        List<SysPicture> pictureList=pictureService.getMainPicture(1);
        //新手标
        List<BasItemVo> isNewItemList =itemService.queryRecomendList();
        //查询2个短标
        List<BasItemVo> shortItemList = itemService.queryRecomendList();
        //查询2个长标
        List<BasItemVo> longItemList = itemService.queryRecomendList();
//        //查询两个债权转让
//        List<BasDebtVO> debtList = debtService.getIndexDebtList();

        Map map=new HashMap();
        map.put("siteId",8);
        Paginator p=new Paginator();
        p.setPageNum(1);
        p.setPageSize(3);
        PageList hyxxArticles=articleService.getList(p, map);

        map.put("siteId",9);
        p.setPageSize(5);
        PageList ptdtArticles=articleService.getList(p,map);

        map.put("siteId",3);
        p.setPageSize(99);
        PageList hzhbArticles=articleService.getList(p,map);

        //获取userId
        Object userId = request.getSession().getAttribute("userId");
        if(userId != null){
            BusAccount account = accountService.getBusAccountByUserId(Integer.parseInt(userId.toString()));
            request.getSession().setAttribute("account",account);
        }

        //统计每个标种的数量
        Map itemNum = null;//itemService.countItemInvestNum();
        //统计可投资债权的数量
        Map debtNum =null;

        request.getSession().setAttribute("shortItemList",shortItemList);
        request.getSession().setAttribute("longItemList",longItemList);
        request.getSession().setAttribute("investAmount",investAmount);
        request.getSession().setAttribute("userNum",userNum);
        request.getSession().setAttribute("pictureList",pictureList);
        request.getSession().setAttribute("isNewItemList",isNewItemList);
        request.getSession().setAttribute("hyxxArticles",hyxxArticles);
        request.getSession().setAttribute("ptdtArticles",ptdtArticles);
        request.getSession().setAttribute("hzhbArticles",hzhbArticles);
        request.getSession().setAttribute("todayAmount",todayAmount);
        request.getSession().setAttribute("repayItemInterest",repayItemInterest);
        request.getSession().setAttribute("itemNum",itemNum);
        request.getSession().setAttribute("debtNum",debtNum);
        return "main/index";
    }

    /**
     * 首页项目列表
     * @param itemType
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/index/itemList", method= RequestMethod.POST)
    public Object itemList(Integer itemType){
       return null; //itemService.getIndexItemList(itemType);
    }



}
